package com.cykj.pos.controller.api;
import com.cykj.pos.util.LoginUserUtils;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cykj.common.annotation.Log;
import com.cykj.common.core.controller.BaseController;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.enums.BusinessType;
import com.cykj.pos.domain.BizMerchAddress;
import com.cykj.pos.service.IBizMerchAddressService;
import com.cykj.common.utils.poi.ExcelUtil;
import com.cykj.common.core.page.TableDataInfo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

/**
 * 伙伴地址Controller
 *
 * @author weijianbo
 * @date 2021-04-28
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/pos/address" )
public class PosV2AppAdressController extends BaseController{
    private final IBizMerchAddressService iBizMerchAddressService ;

    /**
     * 查询伙伴地址列表
     */

    @PostMapping("/list")
    public AjaxResult list(BizMerchAddress bizMerchAddress) {
        AjaxResult ajaxResult = AjaxResult.success();
        String userId = String.valueOf(LoginUserUtils.getLoginUserId());
        bizMerchAddress.setUserId(userId);
        List<BizMerchAddress> list = iBizMerchAddressService.queryList(bizMerchAddress);
        ajaxResult.put("data",list);
        return ajaxResult;
    }

    /**
     * 更改默认地址状态
     * @param bizMerchAddress
     * @return
     */
    @PostMapping("/getType")
    public AjaxResult getType(@RequestBody BizMerchAddress bizMerchAddress) {
        AjaxResult ajaxResult = AjaxResult.success("修改默认地址成功");
        String userId = String.valueOf(LoginUserUtils.getLoginUserId());
        bizMerchAddress.setUserId(userId);
        iBizMerchAddressService.updateDefaultAddr(bizMerchAddress);
        return ajaxResult;
    }

    /**
     * 获取伙伴地址详细信息
     */

    @PostMapping(value = "/findById" )
    public AjaxResult getInfo(@RequestBody BizMerchAddress bizMerchAddress) {
        AjaxResult ajaxResult = AjaxResult.success();
        BizMerchAddress address=iBizMerchAddressService.selectById(bizMerchAddress);
        ajaxResult.put("data",address);
        return ajaxResult;
    }

    /**
     * 新增伙伴地址
     */
    @PostMapping("/save")
    public AjaxResult add(@RequestBody BizMerchAddress bizMerchAddress) {
        String userId=String.valueOf(LoginUserUtils.getLoginUserId());
        LocalDateTime localDate = LocalDateTime.now();
        String formatedDate = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        bizMerchAddress.setUserId(userId);
        bizMerchAddress.setCreatTime(formatedDate);
        List<BizMerchAddress> list = iBizMerchAddressService.List(bizMerchAddress);//根据userid查询地址列表
        if (list.size()==0) {
            bizMerchAddress.setType("1");//默认地址
        } else {
            bizMerchAddress.setType("2");//正常地址
        }
        return toAjax(iBizMerchAddressService.save(bizMerchAddress) ? 1 : 0);
    }

    /**
     * 修改伙伴地址
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BizMerchAddress bizMerchAddress) {
        AjaxResult ajaxResult = AjaxResult.success("修改地址成功");
        BizMerchAddress address= iBizMerchAddressService.updateByAddrId(bizMerchAddress);
        ajaxResult.put("data",address);
        return ajaxResult;
    }

    /**
     * 删除伙伴地址
     */
    @PostMapping("/del" )
    public AjaxResult remove(@RequestBody BizMerchAddress bizMerchAddress) {
        AjaxResult ajaxResult = AjaxResult.success("删除地址成功");
        iBizMerchAddressService.delete(bizMerchAddress);
        return ajaxResult;
    }
}
